<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__stock}">
      <div class="ba-operate-form">
        <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="70px">
          <el-row :gutter="20">
            <el-col :md="6" :sm="6">
              <el-form-item label="砂芯图号">
                <el-input v-model="dataForm.spuNo" placeholder="精确匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="6">
              <el-form-item label="批次">
                <el-input v-model="dataForm.lotNo" placeholder="精确匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :md="6" :sm="6">
              <el-form-item label="托盘条码">
                <el-input v-model="dataForm.contNo" placeholder="精确匹配" maxlength="32" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :md="6" :sm="6">
              <div class="right-handle-group">
                <el-button-group>
                  <el-button @click="getDataList()" type="primary">{{ $t('query') }}</el-button>
                  <el-button @click="getReset()">{{ $t("reset") }}</el-button>
                  <el-tooltip class="item" effect="dark" content="高级搜索" placement="top-start">
                    <el-button @click="advanced = !advanced" color="#dcdfe6" icon="el-icon-search" plain></el-button>
                  </el-tooltip>
                </el-button-group>
              </div>
            </el-col>
          </el-row>
          <el-collapse-transition>
            <div v-show="advanced">
              <el-row :gutter="20">
                <el-col :md="6" :sm="6">
                  <el-form-item label="库存ID">
                    <el-input v-model="dataForm.stockId" placeholder="精确匹配" maxlength="10" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :md="6" :sm="6">
                  <el-form-item label="所在位置">
                    <el-input v-model="dataForm.curPosition" placeholder="精确匹配" maxlength="20" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :md="6" :sm="6">
                  <el-form-item label="库存状态">
                    <el-select v-model="dataForm.stockSt" placeholder="可选状态">
                      <el-option label="空闲" value="0"></el-option>
                      <el-option label="预分配" value="1"></el-option>
                      <el-option label="已使用" value="2"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :md="6" :sm="6">
                  <el-form-item label="是否冻结">
                    <el-select v-model="dataForm.lockSt" placeholder="冻结状态">
                      <el-option label="正常" value="0"></el-option>
                      <el-option label="冻结" value="1"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :md="8" :sm="12">
                  <el-form-item label="创建日期">
                    <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange"
                      range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                      :picker-options="pickerOptions">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-collapse-transition>
        </el-form>
      </div>

      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle"
        style="width: 100%;">
        <el-table-column prop="stockId" label="id" header-align="center" align="center" sortable></el-table-column>
        <el-table-column prop="spuNo" label="砂芯图号" header-align="center" align="center"
          min-width="150"></el-table-column>
        <el-table-column prop="lotNo" label="批次" header-align="center" align="center"></el-table-column>
        <el-table-column prop="contNo" label="托盘条码" header-align="center" align="center"
          min-width="220"></el-table-column>
        <el-table-column prop="curPosition" label="所在位置" header-align="center" align="center" min-width="150">
        </el-table-column>
        <el-table-column prop="spuName" label="砂芯名称" header-align="center" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="stockQnty" label="库存量" header-align="center" align="center"></el-table-column>
        <el-table-column prop="lockedQnty" label="预分配量" header-align="center" align="center"></el-table-column>
        <el-table-column prop="lockSt" label="冻结状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.lockSt" @change="changeStatus($event, scope.row)" active-color="#13ce66"
              inactive-color="#ff4949" :active-value="0" :inactive-value="1"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="stockSt" label="库存状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.stockSt === 0">空闲</el-tag>
            <el-tag v-else-if="scope.row.stockSt === 1" type="warning">已分配</el-tag>
          </template>
        </el-table-column>
      
        <el-table-column prop="creatorName" label="创建用户" header-align="center" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="createDate" label="创建时间" header-align="center" align="center" min-width="155" sortable>
        </el-table-column>
        <el-table-column prop="updaterName" label="更新用户" header-align="center" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="updateDate" label="更新时间" header-align="center" align="center" min-width="155" sortable>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import debounce from 'lodash/debounce'
import mixinViewModule from '@/mixins/view-module'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/wms/stock/page',
        getDataListIsPage: true,
        exportURL: '/wms/stock/export',
        deleteURL: '/wms/stock',
        deleteIsBatch: true
      },
      dataForm: {
        stockId: '',
        contNo: '',
        curPosition: '',
        spuNo: '',
        lotNo: '',
        stockSt: '',
        lockSt: '',
        startDate: '',
        endDate: '',
      },
      changeForm: {
        stockId: '',
        lockSt: ''
      },
      //日期范围
      dateRange: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "当天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setHours(0, 0, 0);

              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          }
        ]
      },
      advanced: false, //高级搜索
    }
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
      this.dateRange = this.$options.data().dateRange;
    },
    //1.库存冻结
    changeStatus: debounce(function (val, row) {
      var tempStatus = row.lockSt === 0 ? 1 : 0;
      this.changeForm.stockId = row.stockId;
      this.changeForm.lockSt = row.lockSt;
      this.$http[!this.changeForm.stockId ? 'post' : 'put']('/wms/stock/', this.changeForm).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.lockSt = tempStatus;
          return this.$message.error(res.msg)
        }
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
          }
        })
      }).catch(() => { })
    })
  }
}
</script>
<style>
.el-row {
  margin-bottom: 0px;
}

.right-handle-group {
  float: right;
}
</style>
